/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.tests;

import com.eng.scc.core.data.City;
import com.eng.scc.core.data.HomeState;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;

/**
 *
 * @author rd
 */
public class test
{

    private Workbook planilha; // objeto que receberÃ¡ um instancia da planilha estudada
    private Sheet aba; // objeto que serÃ¡ a aba
    private File arquivo; // arquivo .xls que serÃ¡ lido

    private HashMap<String, ArrayList<String>> hashMap = new HashMap<String, ArrayList<String>>();
    
    public test()
    {

        try
        {

            arquivo = new File( "c:/Users/Rafael/Desktop/dados.xls" );

// instancia a planilha

            WorkbookSettings conf = new WorkbookSettings();
            conf.setEncoding("ISO-8859-1");
            planilha = Workbook.getWorkbook( arquivo,conf );

//Obendo as Abas da planilha

            Sheet[] abas = planilha.getSheets();

            aba = planilha.getSheet( 0 ); // pega a primeira aba, ou seja, aba de indice 0.

            for ( int i = 0; i < aba.getRows(); i++ )
            {
                Cell[] linha = aba.getRow( i );
                
                ArrayList<String> ses = null;
                
                if ( ( ses = hashMap.get( linha[0].getContents() ) ) == null )
                {
                    hashMap.put( linha[0].getContents(), (ses = new ArrayList<String>()) );
                }
                
                ses.add( linha[1].getContents() );
            }
            
            for ( String s : hashMap.keySet() )
            {
                System.out.println( s + " - " + hashMap.get( s ).size() );
                
                HomeState ref_id = null;
                
                if (( ref_id = FuelControllerManagerService.getInstance().getHomeState( s )) == null )
                {
                    ref_id = new HomeState();
                    ref_id.setName( s );
                    
                    FuelControllerManagerService.getInstance().addHomeState( ref_id );
                }
                
                for ( String v : hashMap.get( s ) )
                {
                    System.out.println( "\t-" + v );
                    
                    City c = new City();
                    c.setHomeStateId( ref_id.getId() );
                    c.setName( v );
                    
                    FuelControllerManagerService.getInstance().addCity( c );
                }
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    public static void main( String[] args )
    {
        new test();
    }
}
